﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ST.BLL;
using ST.Model;

namespace ShowTime
{
    public partial class GoodsMarket : Form
    {
        public GoodsMarket()
        {
            InitializeComponent();
        }

        private void GoodsMarket_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F2) 
            {
                txtSousuo.Focus();
            }
        }

        private void txtSousuo_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (txtSousuo.Text != "")
                {
                    btnSoysuo_Click(null, null);
                }
            }
        }
        private GoodsServiceHlep gs = new GoodsServiceHlep();
        private List<BuyGoodsInfo> buyGoods = new List<BuyGoodsInfo>();

        private void btnSoysuo_Click(object sender, EventArgs e)
        {
            if (txtSousuo.Text != "")
            {
                List<GoodsInfo> list = gs.GetDB(txtSousuo.Text);
                BuyGoodsInfo bg = new BuyGoodsInfo();
                if(list.Count == 0)
                {
                    MessageBox.Show("输入的商品编号无效");
                }
                else if (list.Count == 1)
                {
                    bg.GoodsName = list[0].GoodsName;
                    bg.SalePrice = list[0].SalePrice;
                    bg.Count = 1;
                    buyGoods.Add(bg);
                }
                else
                {
                    BuySelectGoods bsg = new BuySelectGoods(list);
                    bsg.ShowDialog();
                    try
                    {
                        bg.GoodsName = bsg.Goods.GoodsName;
                        bg.SalePrice = bsg.Goods.SalePrice;
                        bg.Count = 1;
                        buyGoods.Add(bg);
                    }
                    catch (Exception)
                    {
                        txtSousuo.Text = "";
                        return;
                    }
                }
                Set();
                txtSousuo.Text = "";
            }
        }

        private void Set() 
        {
            dgvShuju.DataSource = new BindingList<BuyGoodsInfo>(buyGoods);
        }

        private void GoodsMarket_Load(object sender, EventArgs e)
        {
            dgvShuju.AutoGenerateColumns = false;
            
        }
    }
}
